package com.dq.domain.sys.user.user.base;

import com.dq.vo.baseVo.VO;

import com.dq.domain.sys.menu.SysMenu;
import com.dq.domain.position.OrgPosition;
import com.dq.domain.sys.section.SysSection;
import com.dq.utils.TimeUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.shiro.crypto.hash.Sha256Hash;

import javax.persistence.*;
import java.math.BigDecimal;
import java.util.List;
import java.util.Set;
import java.util.UUID;

/**
 * 系统用户
 * @author gewenzhen
 */
@Data
@Entity
@Table(indexes = {@Index(columnList = "cardNo")})
@ApiModel("顶级用户")
public class SysUser extends SysLoginUser implements VO.ToVO {
    @ApiModelProperty(value = "用户编号/工号, 区别于会员卡号", hidden = true)
    protected String cardNo ;
    @ApiModelProperty("用户真实姓名")
    protected String userRealcName;
    @ApiModelProperty("昵称/展示名称")
    protected String localNickName;

    @ManyToOne
    @ApiModelProperty(value = "所属门店", hidden = true)
    public SysSection section;

    @Enumerated(EnumType.ORDINAL)
    @ApiModelProperty("状态")
    protected Status status;

    @ApiModelProperty("头像")
    protected String headImg;
    @ApiModelProperty("手机号")
    protected String mobile;
    @ApiModelProperty("邮箱")
    protected String email;
    @ApiModelProperty("生日")
    protected Long brithday;
    @ApiModelProperty("入职时间/入会时间")
    protected Long entryTime;

    @ApiModelProperty(value = "性别, female(女), male(男)", hidden = true)
    protected Gender gender;

    @ApiModelProperty(value = "推荐人", hidden = true)
    @ManyToOne
    protected SysUser inviteUser;
    @ApiModelProperty(value = "推荐时间")
    protected Long inviteTime;
    @ApiModelProperty("邀请码")
    protected String shareCode;

    @ApiModelProperty("岗位")
    @ManyToOne
    protected OrgPosition orgPosition;

    @ApiModelProperty("省")
    protected String province;
    @ApiModelProperty("市")
    protected String city;
    @ApiModelProperty("区")
    protected String district;


    @ApiModelProperty("充值提成比例")
//    @Column(columnDefinition="decimal(19,2) default 0.00")
    protected BigDecimal topRatio;
    @ApiModelProperty("消耗提成比例")
//    @Column(columnDefinition="decimal(19,2) default 0.00")
    protected BigDecimal expendRatio;

    /*************************  userinfo *****************************/
    @ApiModelProperty("用户微信号")
    protected String wxNumber;

    @ApiModelProperty("身份证号码")
    protected String idCardNo;

    @ApiModelProperty(value = "支付密码，和密码加密方式一样")
    protected String payPwd;

    @ApiModelProperty("工作单位名称")
    protected String employerName;

    public enum Status {
        off("禁用"), on("正常"),;

        Status(String name) {
            this.name = name;
        }

        public String name;
    }
    public enum Gender {
        female("女"), male("男"), xx("未知");

        Gender(String name) {
            this.name = name;
        }

        public String name;
    }
    public static String[] chars = new String[]{"a", "b", "c", "d", "e", "f",
            "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
            "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
            "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
            "W", "X", "Y", "Z"};


    public static String generateShortUuid() {
        StringBuffer shortBuffer = new StringBuffer();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        for (int i = 0; i < 8; i++) {
            String str = uuid.substring(i * 4, i * 4 + 4);
            int x = Integer.parseInt(str, 16);
            shortBuffer.append(chars[x % 0x3E]);
        }
        return shortBuffer.toString();

    }

    @Data
    @ApiModel(value = "用户显示结构")
    public static class Vo implements VO {
        @ApiModelProperty(value = "用户id")
        private Long id;
        @ApiModelProperty(value = "用户编号")
        private String cardNo;
        @ApiModelProperty(value = "用户登陆名")
        private String username;
        @ApiModelProperty("真实姓名")
        protected String userRealcName;
        @ApiModelProperty(value = "邮箱")
        private String email;
        @ApiModelProperty(value = "手机号")
        private String mobile;
        @ApiModelProperty(value = "状态")
        private Status status;
        @ApiModelProperty("状态")
        protected String statusEn;
        @ApiModelProperty("状态")
        protected String statusCn;
        @ApiModelProperty(value = "创建时间")
        private String createTime;
        @ApiModelProperty(value = "更新时间")
        private String updateTime;
        @ApiModelProperty(value = "创建者id")
        private Long createUserId;
        @ApiModelProperty(value = "部门id")
        private Long sysDeptId;
        @ApiModelProperty(value = "部门名称")
        private String sysDeptName;
        @ApiModelProperty(value = "头像")
        private String headImg;
        @ApiModelProperty("token")
        private String token;
        @ApiModelProperty("展示名称")
        private String localNickName;
        @ApiModelProperty(value = "邀请人昵称")
        private String inviteUserNickName;
        @ApiModelProperty(value = "邀请人微信手机号")
        private String inviteUserMobile;

        @ApiModelProperty("省份 set")
        protected String province;
        @ApiModelProperty("城市 set ")
        protected String city;
        @ApiModelProperty("区 set ")
        protected String district;

        @ApiModelProperty(value = "角色id列表")
        private Set<Long> roleIdList;
        @ApiModelProperty(value = "角色code列表")
        private Set<String> roleCodes;
        @ApiModelProperty("菜单id列表")
        private List<SysMenu.Vo> menuList;
        @ApiModelProperty("按钮id列表")
        private Set<SysMenu.Vo> buttonList;
    }

    @Override
    public VO toVO(boolean isRecursion) {
        Vo vo = new Vo();
        vo.id = this.id;
        vo.cardNo = this.cardNo;
        vo.username = this.username;
        vo.userRealcName = fmt(this.getUserRealcName());
        vo.email = this.email;
        vo.mobile = this.mobile;
        vo.status = this.status;
        vo.statusEn = fmt(this.getStatus().toString());
        vo.statusCn = fmt(this.getStatus().name);
        vo.updateTime = TimeUtil.longToString(this.lastModifyTime, TimeUtil.YYYY_MM_DD_HH_MM_SS);
        vo.createTime = TimeUtil.longToString(this.createTime, TimeUtil.YYYY_MM_DD_HH_MM_SS);
        vo.createUserId = this.createUser == null ? null : this.createUser.getId();
        vo.sysDeptId = this.section == null ? null : this.section.getId();
        vo.sysDeptName = this.section == null ? "" : this.section.getSimpleName();
        vo.headImg = fmt(this.headImg);
        vo.localNickName = this.localNickName;

        return vo;
    }

    public static String generatePaw(String password) {
        return new Sha256Hash(password).toHex();
    }
}
